class User < ActiveRecord::Base
  has_many :sql_orders
  has_many :deploy_histories
  has_many :db_files
  validates :username, presence: true,
            length: 3..20,
            uniqueness: true
  validates :password, presence: true,
            length: 6..20,
            confirmation: true
  #validates :product_line, presence: true
  before_create :encrypt_password

  def encrypt_password
    self.password = Digest::MD5.hexdigest(self.password)
  end
 
  #是否能是admin
  def is_admin?
    self.permissions && self.permissions.index('admin')
  end
  #是否能部署生产
  def can_deploy_production?
    self.permissions && (self.permissions.index('deploy_production') or self.permissions.index('admin'))
  end
  #是否能部署上线前
  def can_deploy_predeploy?
    self.permissions && (self.permissions.index('deploy_predeploy') or self.permissions.index('admin'))
  end
  #是否能部署灰度
  def can_deploy_huidu?
    self.permissions && (self.permissions.index('deploy_huidu') or self.permissions.index('admin'))
  end
  #是否能部署集成
  def can_deploy_jicheng?
    self.permissions && (self.permissions.index('deploy_jicheng') or self.permissions.index('admin'))
  end
  #是否能审核数据库上线单
  def can_audited?
    self.permissions && (self.permissions.index('admin') or self.permissions.index('leader'))
  end
  #是否能取消数据库上线单
  def can_cancel?
    self.permissions && self.permissions.index('admin')
  end
  #是否能立即执行数据库上线单
  def can_execute_now?
    self.permissions && self.permissions.index('admin')
  end
  #是否能列出所有的应用程序及部署历史
  def can_list_all_app_his?
    self.permissions && (self.permissions.index('admin') or self.permissions.index('tester'))
  end
  #是否能看到所有的部署历史
  def can_list_all_his?
    self.permissions && self.permissions.index('admin')
  end
end
